package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * projectName: com.atguigu.lease.web.admin.schedule
 *
 * @author: 赵伟风
 * description: 封装定时任务方法的类
 */

@Component
public class MyTask {


    //按天扫描的定时方法，检查租约是否过期
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void checkAgreement(){
        //修改租约状态即可
        LambdaUpdateWrapper<LeaseAgreement> leaseAgreementLambdaUpdateWrapper = new LambdaUpdateWrapper<>() ;
        //条件1： status 2 or 5   in (2,5)
        //leaseAgreementLambdaUpdateWrapper.eq(LeaseAgreement::getStatus, LeaseStatus.SIGNED).or().eq(LeaseAgreement::getStatus,LeaseStatus.WITHDRAWING);
        leaseAgreementLambdaUpdateWrapper.in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING);
        //条件2：last_time < now
        leaseAgreementLambdaUpdateWrapper.lt(LeaseAgreement::getLeaseEndDate,new Date());
        //修改值： status = 4
        leaseAgreementLambdaUpdateWrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        leaseAgreementService.update(leaseAgreementLambdaUpdateWrapper);

    }

}
